<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="地址ID" prop="id">
        <el-input v-model="dataForm.id" placeholder="地址ID"></el-input>
      </el-form-item>
      <el-form-item label="用户ID" prop="userId">
        <el-input v-model="dataForm.userId" placeholder="用户ID"></el-input>
      </el-form-item>
      <el-form-item label="收件人姓名" prop="consignee">
        <el-input v-model="dataForm.consignee" placeholder="收件人姓名"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-input v-model="dataForm.sex" placeholder="性别"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item label="省份编码" prop="provinceCode">
        <el-input v-model="dataForm.provinceCode" placeholder="省份编码"></el-input>
      </el-form-item>
      <el-form-item label="省份名称" prop="provinceName">
        <el-input v-model="dataForm.provinceName" placeholder="省份名称"></el-input>
      </el-form-item>
      <el-form-item label="城市编码" prop="cityCode">
        <el-input v-model="dataForm.cityCode" placeholder="城市编码"></el-input>
      </el-form-item>
      <el-form-item label="城市名称" prop="cityName">
        <el-input v-model="dataForm.cityName" placeholder="城市名称"></el-input>
      </el-form-item>
      <el-form-item label="区县编码" prop="districtCode">
        <el-input v-model="dataForm.districtCode" placeholder="区县编码"></el-input>
      </el-form-item>
      <el-form-item label="区县名称" prop="districtName">
        <el-input v-model="dataForm.districtName" placeholder="区县名称"></el-input>
      </el-form-item>
      <el-form-item label="详细地址" prop="detail">
        <el-input v-model="dataForm.detail" placeholder="详细地址"></el-input>
      </el-form-item>
      <el-form-item label="逻辑删除：1-已删除，0-未删除" prop="deleted">
        <ren-radio-group v-model="dataForm.deleted" dict-type="is_active"></ren-radio-group>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  userId: "",
  consignee: "",
  sex: "",
  phone: "",
  provinceCode: "",
  provinceName: "",
  cityCode: "",
  cityName: "",
  districtCode: "",
  districtName: "",
  detail: "",
  deleted: "",
});

const rules = ref({
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = id;

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/sys/addressbook/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/sys/addressbook", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>